import { legacy_createStore as createStore } from 'redux';

// 初始值
const initState = { count: 100 };

// reducer 函数
function countReducer(state = initState, { type, payload }) {
  //   console.log('state,action -----> ', state, action);
  switch (type) {
    case 'add':
      return {
        ...state,
        count: state.count + payload,
      };
    case 'des':
      return {
        ...state,
        count: state.count - payload,
      };
    default:
      return state;
  }
}

// 把 action 独立出来，方便维护，减少代码书写和错误
export function addAction(num) {
  return {
    type: 'add',
    payload: num,
  };
}
export function desAction(num) {
  return {
    type: 'des',
    payload: num,
  };
}

// 创建store： createStore(reducer函数)
const store = createStore(countReducer);

export default store;
